require( "datastream" ) 
if SERVER then AddCSLuaFile("cl_Menu.lua") return end
Toggled = false
Opened = false
ToggleClient = 0
QueueList = {}
Colr = {50,50,50,50,50}
MuteMusic = false
FirstToggle = 0
// sdfdsf
if (Site == nil) then
	Site = vgui.Create("HTML")
	Site:SetPos(100,100)
	Site:SetSize(300,80)
	Site:SetVisible(false)
end

function OpenMenu()
Prawr = 0
if (Opened == false) then
Opened = true
//=================================================//
		  --MAIN SETUP--
//=================================================//
MainPanel = vgui.Create( "DFrame" )
MainPanel:SetBackgroundBlur( true )
MainPanel:SetPos((ScrW() / 2) - 260,(ScrH() / 2) - 300)
MainPanel:SetSize( 520, 630 )
MainPanel:SetTitle( "" )
MainPanel:SetVisible( true )
MainPanel:MakePopup()
MainPanel:SetDraggable( false )
MainPanel:ShowCloseButton( false )
MainPanel.Paint = function()
	draw.RoundedBox(6,0,0,MainPanel:GetWide(), MainPanel:GetTall(),Color(168,168,168,255))
	draw.RoundedBox(6,1,1,MainPanel:GetWide()-2, MainPanel:GetTall()-2,Color(25,25,25,230))
end

local PropertySheet = vgui.Create( "DPropertySheet" )
PropertySheet:SetParent( MainPanel )
PropertySheet:SetPos( 3, 3 )
PropertySheet:SetSize( 514, 624 )

local CloseButton = vgui.Create("DButton",PropertySheet)
CloseButton:SetPos(494,0)
CloseButton:SetSize(20,20)
CloseButton:SetText("")
CloseButton.Paint = function()
	draw.RoundedBox(4,0,0, CloseButton:GetWide(), CloseButton:GetTall(),Color(168,168,168,255))
	--surface.SetDrawColor( 168, 168, 168, 255 )
    --surface.DrawRect( 0, 0, CloseButton:GetWide(), CloseButton:GetTall() )
	surface.SetFont("DefaultBold")
	surface.SetTextColor(25,25,25,255)
	surface.SetTextPos(7,3)
	surface.DrawText("X")
end
CloseButton.DoClick = function()
	MainPanel:SetVisible(false)
	Toggled = false
end
 
MenuText= vgui.Create("DLabel", PropertySheet)
MenuText:SetPos(180,3)
MenuText:SetText("PO Radio by Spencer Henslol. Total Songs Available ---")
MenuText:SizeToContents() // make the control the same size as the text.


//=================================================//
		 -- SONG PANEL--
//=================================================//
local SongPanel = vgui.Create( "DPanel" )
SongPanel:SetPos(0,0)
SongPanel:SetSize(20,20)
SongPanel.Paint = function()
    draw.RoundedBox( 6,0, 0, SongPanel:GetWide(), SongPanel:GetTall()-78,Color(50,50,50,255) )
	draw.RoundedBox( 6,0, SongPanel:GetTall() - 73, SongPanel:GetWide(), 40,Color(50,50,50,255) )
	draw.RoundedBox( 6,SongPanel:GetWide() - 130, SongPanel:GetTall() - 27, 130, 25,Color(50,50,50,255) )
end

MusicWindow = vgui.Create("DListView")
MusicWindow:SetParent(SongPanel)
MusicWindow:SetPos(5,2)
MusicWindow:SetSize(495, 510)
MusicWindow:SetMultiSelect(false)
local Name = MusicWindow:AddColumn("Song Name")
local Artist = MusicWindow:AddColumn("Artist")
local Length = MusicWindow:AddColumn("Time")
	
Name:SetWidth(280)
Artist:SetWidth(165)
Length:SetWidth(25)

	local OldClickLine = MusicWindow.OnClickLine;
	
		MusicWindow.OnClickLine = function(parent,line,isselected)
			OldClickLine(parent,line,isselected);
			selectzor(line)
		end
		
		function selectzor(cawk)
		SongPath1 = cawk:GetValue(1)
		SongArtist1 = cawk:GetValue(2)
		SongTime1 = cawk:GetValue(3)
		end
		
if Toggled == false then
	MainPanel:MakePopup()
	MainPanel:SetVisible( true )
	MainPanel:SetKeyboardInputEnabled(true)
	Toggled = true
	//LoadList()
else
	MainPanel:SetVisible(false)
	Toggled = false
end

PlayButton = vgui.Create("DButton")
PlayButton:SetParent(SongPanel)
PlayButton:SetPos(5,525)
PlayButton:SetSize(130,30)
PlayButton:SetText("Add Selected To Queue")
PlayButton.OnCursorEntered = function()
Colr[1] = 90
end
PlayButton.OnCursorExited = function()
Colr[1] = 50
end
PlayButton.OnMousePressed = function()
Colr[1] = 130
end
PlayButton.OnMouseReleased = function()
Colr[1] = 90
if (ToggleClient == 1) then
	Site:SetHTML("<iframe src=\"http://decrepitgaming.com/addons/radio/song.php?artist="..SongArtist1.."&song="..SongPath1.."\" />")
else
	if(SongArtist1 != nil and SongPath1 != nil and SongTime1 != nil) then
		RunConsoleCommand("PO-addsong",SongArtist1.." - "..SongPath1..".mp3|"..SongTime1)
	end
end
end
PlayButton.Paint = function()
	draw.RoundedBox(6,0,0,PlayButton:GetWide(), PlayButton:GetTall(),Color(168,168,168,255))
	draw.RoundedBox(6,1,1,PlayButton:GetWide()-2, PlayButton:GetTall()-2,Color(Colr[1],Colr[1],Colr[1],255))
end
//+++++++++++++++++++++++++++++++++++++================================
//+++++++++++++++++++++++++++++++++++++================================

StopButton = vgui.Create("DButton")
StopButton:SetParent(SongPanel)
StopButton:SetPos(140,525)
StopButton:SetSize(90,30)
StopButton:SetText("Stop This Song!")
StopButton.OnCursorEntered = function()
Colr[2] = 90
end
StopButton.OnCursorExited = function()
Colr[2] = 50
end
StopButton.OnMousePressed = function()
Colr[2]= 130
end
StopButton.OnMouseReleased = function()
Colr[2] = 90
Site:SetHTML("")
end
StopButton.Paint = function()
	draw.RoundedBox(6,0,0,StopButton:GetWide(), StopButton:GetTall(),Color(168,168,168,255))
	draw.RoundedBox(6,1,1,StopButton:GetWide()-2, StopButton:GetTall()-2,Color(Colr[2],Colr[2],Colr[2],255))
end

//+++++++++++++++++++++++++++++++++++++================================
//+++++++++++++++++++++++++++++++++++++================================

RefreshButton = vgui.Create("DButton")
RefreshButton:SetParent(SongPanel)
RefreshButton:SetPos(235,525)
RefreshButton:SetSize(120,30)
RefreshButton:SetText("Refresh The Song List")
RefreshButton.OnCursorEntered = function()
Colr[3] = 90
end
RefreshButton.OnCursorExited = function()
Colr[3] = 50
end
RefreshButton.OnMousePressed = function()
Colr[3] = 130
end
RefreshButton.OnMouseReleased = function()
Colr[3] = 90
	RefreshList()
	LocalPlayer():PrintMessage(HUD_PRINTTALK, "Refreshing Song List...")
end
RefreshButton.Paint = function()
	draw.RoundedBox(6,0,0,RefreshButton:GetWide(), RefreshButton:GetTall(),Color(168,168,168,255))
	draw.RoundedBox(6,1,1,RefreshButton:GetWide()-2, RefreshButton:GetTall()-2,Color(Colr[3],Colr[3],Colr[3],255))
end

//+++++++++++++++++++++++++++++++++++++================================
//+++++++++++++++++++++++++++++++++++++================================

SwitchButton = vgui.Create("DButton")
SwitchButton:SetParent(SongPanel)
SwitchButton:SetPos(360,525)
SwitchButton:SetSize(140,30)
SwitchButton:SetText("Switch To Client-Side Player")
SwitchButton.OnCursorEntered = function()
Colr[4] = 90
end
SwitchButton.OnCursorExited = function()
Colr[4] = 50
end
SwitchButton.OnMousePressed = function()
Colr[4] = 130
end
SwitchButton.OnMouseReleased = function()
Colr[4] = 90
if (ToggleClient == 1) then
	ToggleClient = 0
	SwitchButton:SetText("Switch To Client-Side Player")
	PlayButton:SetText("Add Selected To Queue")
		LocalPlayer():PrintMessage(HUD_PRINTTALK, "Switching to Server-Side Radio")
	MuteMusic = false
else
	if string.find( LocalPlayer():GetNetworkedString( "flags" ), "T" ) then
		ToggleClient = 1
		SwitchButton:SetText("Switch To Server-Side Radio")
		PlayButton:SetText("Play The Selected Song")
			LocalPlayer():PrintMessage(HUD_PRINTTALK, "Switching to Client-Side Player")
		MuteMusic = true
	else
		LocalPlayer():PrintMessage( HUD_PRINTTALK, "You lack the necessary flags to switch to the clientside player (T)." )
	end
end
end
SwitchButton.Paint = function()
	draw.RoundedBox(6,0,0,SwitchButton:GetWide(), SwitchButton:GetTall(),Color(168,168,168,255))
	draw.RoundedBox(6,1,1,SwitchButton:GetWide()-2, SwitchButton:GetTall()-2,Color(Colr[4],Colr[4],Colr[4],255))
end

local MuteAll = vgui.Create("DCheckBoxLabel")
MuteAll:SetParent(SongPanel)
MuteAll:SetPos(390,571)
MuteAll:SetWide(150)
MuteAll:SetText("Mute All Songs?")
MuteAll.OnChange = function()
	MuteMusic = MuteAll:GetChecked()
end
 
 
 //=================================================//
		 -- QUEUE PANEL--
//=================================================//
 
local QueuePanel = vgui.Create( "DPanel" )
QueuePanel:SetPos(0,0)
QueuePanel:SetSize(20,20)
QueuePanel.Paint = function()
    draw.RoundedBox( 6,0, 0, QueuePanel:GetWide(), 193,Color(50,50,50,255) )
	draw.RoundedBox( 6,0, 198, 170, 40,Color(50,50,50,255) )
end

RemoveButton = vgui.Create("DButton")
RemoveButton:SetParent(QueuePanel)
RemoveButton:SetPos(5,203)
RemoveButton:SetSize(160,30)
RemoveButton:SetText("Remove Selected Song")
RemoveButton.OnCursorEntered = function()
	Colr[5] = 90
end
RemoveButton.OnCursorExited = function()
	Colr[5] = 50
end
RemoveButton.OnMousePressed = function()
	Colr[5] = 130
end
RemoveButton.OnMouseReleased = function()
	Colr[5] = 90
	RunConsoleCommand("PO-Remove",SongArtist2.." - "..SongPath2..".mp3|"..SongTime2)
end
RemoveButton.Paint = function()
	draw.RoundedBox(6,0,0,RemoveButton:GetWide(), RemoveButton:GetTall(),Color(168,168,168,255))
	draw.RoundedBox(6,1,1,RemoveButton:GetWide()-2, RemoveButton:GetTall()-2,Color(Colr[5],Colr[5],Colr[5],255))
end


QueueWindow = vgui.Create("DListView")
QueueWindow:SetParent(QueuePanel)
QueueWindow:SetPos(5,2)
QueueWindow:SetSize(495, 187)
QueueWindow:SetMultiSelect(false)
local Name = QueueWindow:AddColumn("Song Name")
local Artist = QueueWindow:AddColumn("Artist")
local Length = QueueWindow:AddColumn("Time")
	
Name:SetWidth(280)
Artist:SetWidth(165)
Length:SetWidth(25)
 
 	local OldClickLine1 = QueueWindow.OnClickLine;
	
		QueueWindow.OnClickLine = function(parent,line,isselected)
			OldClickLine1(parent,line,isselected);
			selectzor1(line)
		end
		
		function selectzor1(cawk)
		SongPath2 = cawk:GetValue(1)
		SongArtist2 = cawk:GetValue(2)
		SongTime2 = cawk:GetValue(3)
		end

PropertySheet:AddSheet( "Song List", SongPanel, "gui/silkicons/application_view_detail", false, false, "Song List Yo." )
PropertySheet:AddSheet( "Queue List", QueuePanel, "gui/silkicons/application_view_tile", false, false, "Dat's da QUEUEUE" )

RefreshList()
else
MainPanel:SetVisible(true)
end
end

function RefreshList()
SongList = {}
	MusicWindow:Clear()
	MenuText:SetText("PO Radio by Spencer Henslol. Refreshing...")
	MusicWindow:AddLine("Refreshing List...")
		http.Get("http://www.decrepitgaming.com/addons/radio/getsongs.php", "", function(Content)
			for k,v in pairs(string.Explode("\n", Content)) do
				local TempTable = {v}
			if table.ToString(TempTable) != "{\"\",}" then
				table.Add(SongList,TempTable)
				Prawr = Prawr + 1
				MenuText:SetText("PO Radio by Spencer Henslol. Songs available: "..Prawr)
			end
			end
		MusicWindow:Clear()
		LoadList(SongList,MusicWindow,"MusicWindow")
		end)
end

function LoadList(listname,panel,name)
	Prawr = 0
	panel:Clear()
	for k,v in pairs(listname) do
		local Explode = string.Explode("|", v)
		local Explode2 = string.Explode("-",Explode[1])
		local ExplodeT = string.Explode(":",Explode[2])
				if(Explode and table.Count(Explode) >1) then
					local Name = string.Trim(Explode2[2])
					local Artist = string.Trim(Explode2[1])
					local Min = string.Trim(ExplodeT[1])
					local Sec = string.Trim(ExplodeT[2])
					local Name = string.gsub(Name,".mp3","")
					if(Name != "" and Artist != "" and Min != "" and Sec != "") then
						panel:AddLine(Name,Artist,Min..":"..Sec)
						if name == "MusicWindow" then
						MusicWindow:SortByColumn(2)
						end
					end
				end
	end
end


datastream.Hook( "QueueTable", function( id, tag, enc, dec )
	LoadList(dec,QueueWindow)
end ) 

datastream.Hook( "SongName", function( id, tag, enc, dec )
	print(dec[1])
		local Explode = string.Explode("|", dec[1])
		local Explode2 = string.Explode("-",Explode[1])
		local ExplodeT = string.Explode(":",Explode[2])
				if(Explode and table.Count(Explode) >1) then
					 sName = string.Trim(Explode2[2])
					 sArtist = string.Trim(Explode2[1])
					 sMin = string.Trim(ExplodeT[1])
					 sSec = string.Trim(ExplodeT[2])
					 sName = string.gsub(sName,".mp3","")
				end
		
		if MuteMusic == false then
			Site:SetHTML("<iframe src=\"http://decrepitgaming.com/addons/radio/song.php?artist="..sArtist.."&song="..sName.."\" />")
		end
end ) 

function PlaySound(name)
	RunConsoleCommand("playgamesound", name)
end

concommand.Add("testpoop",OpenMenu)